package com.iaiai.tm.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iaiai.tm.activity.widget.TableView;

/**
 * 
 * <p>
 * Title: ProcessListView.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2012-2-9 下午3:33:59
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class ProcessLinearLayout extends LinearLayout {

	private Context context;
	
	private List<String[]> list;
	
	private String[] title;
	
	private TableView listView;
	
	private boolean stop = true;

	/**
	 * @param context
	 * @param attrs
	 */
	public ProcessLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);

		this.context = context;
		
		init();
	}

	/**
	 * @param context
	 */
	public ProcessLinearLayout(Context context) {
		super(context);

		this.context = context;
		
		init();
	}
	
	private void init(){
		list = getData();
		
		listView = new TableView(context, new String[]{}, list);
		
		LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.HORIZONTAL);
		
		TextView btn1 = new TextView(context);
		btn1.setBackgroundColor(Color.WHITE);
		btn1.setTextColor(Color.BLACK);
		btn1.setText("开始刷新");
		btn1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(stop) return;
				stop = true;
				readData();
			}
		});
		layout.addView(btn1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1));
		
		TextView btn2 = new TextView(context);
		btn2.setBackgroundColor(Color.WHITE);
		btn2.setTextColor(Color.BLACK);
		btn2.setGravity(Gravity.RIGHT);
		btn2.setText("停止刷新");
		btn2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				stop = false;
			}
		});
		layout.addView(btn2,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1));
		
		setOrientation(LinearLayout.VERTICAL);
		addView(layout,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		addView(listView,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
	}
	
	/**
	 * 返回所有运行的程序
	 * 
	 * @return
	 */
	private List<String[]> getData() {
		List<String[]> l = new ArrayList<String[]>();
		readData();
		return l;
	}

	/**
	 * 返回所有运行的程序
	 * 
	 * @return
	 */
	private void readData() {
		final Handler handler = new Handler();
		new Thread() {
			public void run() {
				while(stop){
					list = new ArrayList<String[]>();
					String data = null;
					
					try {
						data = CMDExecute.run(new String[]{ "/system/bin/top", "-n", "1" }, "/system/bin/");
						while(data.indexOf("  ")>0){
							data = data.replaceAll("  ", " ");
						}
						data = data.replaceAll("  ", " ");
					} catch (IOException ex) {
						ex.printStackTrace();
					}
					final String t = data;
					handler.post(new Runnable() {
						public void run() {
							if(t!=null){
								String[] str = t.split("\n");
								int i = 0;
								for(String s:str){
									if(s.trim().length()<=0){
										continue;
									}
									if(i<2){
										i++;
										continue;
									}
									
									String[] items = s.split(" ");
									if(i==2){
										List<String> l1 = new ArrayList<String>();
										for(int j=0;j<items.length;j++){
											if(j==3||j==4||j==7||j==6||j==5)continue;
											String item=items[j];
											if(item.trim().length()<=0){
												continue;
											}
											l1.add(item);
										}
										title = new String[l1.size()];
										l1.toArray(title);
										i++;
										continue;
									}
									i++;
									List<String> temp = new ArrayList<String>();
									for(int j=0;j<items.length;j++){
										if(j==3||j==4||j==7||j==6||j==5)continue;
										String item=items[j];
										if(item.trim().length()<=0){
											continue;
										}
										temp.add(item.trim());
									}
									
									String[] z = new String[temp.size()];
									temp.toArray(z);
									if(z.length>=4&&isNumeric(z[0])&&z[1].indexOf("%")>0&&z[2].indexOf(".")<0){
										list.add(z);
									}
								}
								removeView(listView);
								listView = new TableView(context, title, list);
								addView(listView,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
							}
						}
					});
				}
			}
		}.start();
	}
	
	/**
	 * 判断是否是数字
	 * @param str
	 * @return
	 */
	private static boolean isNumeric(String str){
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}
	
}
